package ch.simplix.android.remoteZapper;

import java.math.BigInteger;
import java.util.concurrent.TimeUnit;

import android.content.ComponentName;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import ch.simplix.android.digest.DigestHelper;
import ch.simplix.android.repetitive.service.implementation.ARepetitiveService;
import ch.simplix.android.speech.string.FrenchString;
import ch.simplix.android.speech.string.GermanString;
import ch.simplix.android.speech.string.UKString;

public class RemoteZapperService extends ARepetitiveService {
	private static final BigInteger destructionKey = new BigInteger(
			"9d28ec8a787bd9be46bb26863e6e770a4e5f57f3", 16); // destroyMe
	private static final BigInteger haltKey = new BigInteger(
			"da2d54cee2f6944883dcb5ccb2971173ff512a3a", 16); // stopDestroyMe

	private long countdown;
	private Intent speechIntent;

	public RemoteZapperService() {
		speechIntent = new Intent("ch.simplix.android.speech.SPEECH_SERVICE");
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		intent.removeExtra("stop");
		intent.removeExtra("start");
		if (DigestHelper.isEqual(intent.getExtras().getString("message"),
				destructionKey)) {
			intent.putExtra("start", true);
		}
		if (DigestHelper.isEqual(intent.getExtras().getString("message"),
				haltKey)) {
			intent.putExtra("stop", true);
		}

		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onStart(Intent intent) {
		initiateAutoDestruction();
	}

	@Override
	public void onStop() {
		resetAutoDestruction();
	}

	@Override
	public void onRepeat() {
		Log.d("RemoteZapperService", "onRepeat");
		countDown();
	}

	private void resetAutoDestruction() {
		if (countdown == 0)
			return;
		countdown = 0;
		speechIntent.putExtra("Phrase", new UKString(
				"Self destruction sequence aborted."));
		startService(speechIntent);
		speechIntent.putExtra("Phrase", new GermanString(
				"Selbstzerstörungssequenz abgebrochen."));
		startService(speechIntent);
		speechIntent.putExtra("Phrase", new FrenchString(
				"Séquence d'autodestruction avortée."));
		startService(speechIntent);

	}

	private void initiateAutoDestruction() {
		if (countdown != 0)
			return;
		countdown = System.currentTimeMillis() + 3 * 60 * 1000;
		speechIntent.putExtra("Phrase", new UKString(
				"Self destruction sequence initiated."));
		startService(speechIntent);
		speechIntent.putExtra("Phrase", new GermanString(
				"Selbstzerstörungs Sequenz eingeleitet."));
		startService(speechIntent);
		speechIntent.putExtra("Phrase", new FrenchString(
				"Séquence d'autodestruction declanchée."));
		startService(speechIntent);
	}

	private void countDown() {
		int timeLeft = (int) ((countdown - System.currentTimeMillis()) / 1000);
		if (timeLeft > 70) {
			super.setUpdateInterval(60, TimeUnit.SECONDS);
			minutesCount(timeLeft);
		} else if (timeLeft > 11) {
			super.setUpdateInterval(10, TimeUnit.SECONDS);
			secondsBlockCount(timeLeft);
		} else if (timeLeft > 0) {
			super.setUpdateInterval(1, TimeUnit.SECONDS);
			secondsCount(timeLeft);
		} else {
			destroy();
		}
	}

	private void minutesCount(int timeLeft) {
		speechIntent.putExtra("Phrase", new UKString(""
				+ (1 + (timeLeft / (60))) + "Minutes until self destruction"));
		startService(speechIntent);
		speechIntent
				.putExtra("Phrase", new GermanString(""
						+ (1 + (timeLeft / (60)))
						+ "Minuten bis zur Selbstzerstörung"));
		startService(speechIntent);
		speechIntent.putExtra("Phrase", new FrenchString(""
				+ (1 + (timeLeft / (60)))
				+ "minutes jusqu'à ce que l'auto-destruction"));
		startService(speechIntent);
	}

	private void secondsBlockCount(int timeLeft) {
		if (timeLeft % 3 == 0) {
			speechIntent.putExtra("Phrase", new UKString(""
					+ (10 + (timeLeft / 10 * 10))
					+ "Seconds until self destruction"));
		} else if (timeLeft % 3 == 1) {
			speechIntent.putExtra("Phrase", new GermanString(""
					+ (10 + (timeLeft / 10 * 10))
					+ "Sekunden bis zur Selbstzerstörung"));
		} else {
			speechIntent.putExtra("Phrase", new FrenchString(""
					+ (10 + (timeLeft / 10 * 10))
					+ "seconds jusqu'à ce que l'auto-destruction"));
		}
		startService(speechIntent);
	}

	private void secondsCount(int timeLeft) {
		speechIntent.putExtra("Phrase", new UKString("" + (timeLeft)));
		startService(speechIntent);
	}

	private void destroy() {
		countdown = 0;
		super.stopTimedRepetition();
		speechIntent.putExtra("Phrase", new UKString("Self destroying now."));
		startService(speechIntent);
	}

	@Override
	public IBinder onBind(Intent intent) {
		// No Direct Service binding
		return null;
	}

}
